package org.jamin.lending.tool;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

public class AlidayuAchieve {

    private String appKey;

    private String secret;

    public void sendSms() throws IOException {
        List<BasicNameValuePair> basicNameValuePairs = new ArrayList<BasicNameValuePair>();
        Map<String,String> stringMap = new HashMap<String, String>();
        stringMap.put("method","alibaba.aliqin.fc.sms.num.send");
        stringMap.put("app_key",this.appKey);
        stringMap.put("timestamp", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").toString());
        stringMap.put("format","json");
        stringMap.put("v","2.0");
        stringMap.put("sign_method","md5");
        String[] keys = stringMap.keySet().toArray(new String[0]);
        Arrays.sort(keys);
        StringBuilder stringBuilder = new StringBuilder();
        for(String key :keys){
            String value = stringMap.get(key);
            stringBuilder.append(key).append(value);
            basicNameValuePairs.add(new BasicNameValuePair(key,value));
        }
        HttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost("https://eco.taobao.com/router/rest");
        basicNameValuePairs.add(new BasicNameValuePair("sign", DigestUtils.md5Hex(stringBuilder.toString())));
        httpPost.setEntity(new UrlEncodedFormEntity(basicNameValuePairs));
        httpClient.execute(httpPost);
    }

    public AlidayuAchieve(String appKey, String secret) {
        this.appKey = appKey;
        this.secret = secret;
    }
}
